package com.xbongbong.saas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.saas.enums.base.CustomerStatementBaseEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户对账单枚举
 * @author Xbb
 */
@Getter
public enum CustomerStatementEnum {
    //对账日期
    CHECKING_DATE(CustomerStatementBaseEnum.CHECKING_DATE.getAttr(),"enum.customerStatementEnum.attrName.checkingDate","checkingDate",FieldTypeEnum.DATETIME.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),10000,1,0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //类型 - 业务类型 注意：data中此字段已经提到宽表字段 sys_long_1,后面不在使用！！！
    BUSINESS_TYPE(CustomerStatementBaseEnum.BUSINESS_TYPE.getAttr(),"enum.customerStatementEnum.attrName.businessType","businessType",FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9990,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //单据类型，比如回款单类型(对应PaymentSheetTypeEnum)
    TYPE(CustomerStatementBaseEnum.TYPE.getAttr(),"enum.customerStatementEnum.attrName.type","type",FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.NONE.getCode(),9985,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //对账单据 注意：data中此字段已经提到宽表字段 sys_long_2,后面不在使用！！！
    STATEMENT_ID(CustomerStatementBaseEnum.STATEMENT_ID.getAttr(),"enum.customerStatementEnum.attrName.statementId","statementId",FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9980,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //对账单据编号
    STATEMENT_NO(CustomerStatementBaseEnum.STATEMENT_NO.getAttr(),"enum.customerStatementEnum.attrName.statementNo","statementNo",FieldTypeEnum.TEXT.getType(),SetTypeEnum.DISABLE_ALL.getCode(),0,1,ShowTypeEnum.NONE.getCode(),9970,1,1,ScreenTypeEnum.SCREEN_OFF.getType()),
    //关联客户
    CUSTOMER_ID(CustomerStatementBaseEnum.CUSTOMER_ID.getAttr(),"enum.customerStatementEnum.attrName.customerId","customerId",FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.DETAIL.getCode(),9960,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //关联客户名称
    CUSTOMER_NAME(CustomerStatementBaseEnum.CUSTOMER_NAME.getAttr(),"enum.customerStatementEnum.attrName.customerName","customerName",FieldTypeEnum.TEXT.getType(),SetTypeEnum.DISABLE_ALL.getCode(),0,1,ShowTypeEnum.NONE.getCode(),9950,1,1,ScreenTypeEnum.SCREEN_OFF.getType()),
    //关联合同
    CONTRACT_ID(CustomerStatementBaseEnum.CONTRACT_ID.getAttr(),"enum.customerStatementEnum.attrName.contractId","contractId",FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9940,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //关联合同编号
    CONTRACT_NO(CustomerStatementBaseEnum.CONTRACT_NO.getAttr(),"enum.customerStatementEnum.attrName.contractNo","contractNo",FieldTypeEnum.TEXT.getType(),SetTypeEnum.DISABLE_ALL.getCode(),0,1,ShowTypeEnum.NONE.getCode(),9930,1,1,ScreenTypeEnum.SCREEN_OFF.getType()),
    //来源单据类型
    SOURCE_TYPE(CustomerStatementBaseEnum.SOURCE_TYPE.getAttr(),"enum.customerStatementEnum.attrName.sourceType","sourceType",FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,1,ShowTypeEnum.NONE.getCode(),99200,1,0,ScreenTypeEnum.SCREEN_OFF.getType()),
    //来源单据
    SOURCE_ID(CustomerStatementBaseEnum.SOURCE_ID.getAttr(),"enum.customerStatementEnum.attrName.sourceId","sourceId",FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9920,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //来源单据编号
    SOURCE_NO(CustomerStatementBaseEnum.SOURCE_NO.getAttr(),"enum.customerStatementEnum.attrName.sourceNo","sourceNo",FieldTypeEnum.TEXT.getType(),SetTypeEnum.DISABLE_ALL.getCode(),0,1,ShowTypeEnum.NONE.getCode(),9910,1,1,ScreenTypeEnum.SCREEN_OFF.getType()),
    //其它费用
    OTHER_EXPENSE(CustomerStatementBaseEnum.OTHER_EXPENSE.getAttr(),"enum.customerStatementEnum.attrName.other_expense","otherExpense",FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9905,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //应收金额
    AMOUNT_RECEIVABLE(CustomerStatementBaseEnum.AMOUNT_RECEIVABLE.getAttr(),"enum.customerStatementEnum.attrName.amountReceivable","amountReceivable",FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9900,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //回款金额
    AMOUNT_RECEIVED(CustomerStatementBaseEnum.AMOUNT_RECEIVED.getAttr(),"enum.customerStatementEnum.attrName.amountReceived","amountReceived",FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9890,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //核销金额
    WRITE_OFF_AMOUNT(CustomerStatementBaseEnum.WRITE_OFF_AMOUNT.getAttr(),"enum.customerStatementEnum.attrName.writeOffAmount","writeOffAmount",FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9880,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //应收余额
    BALANCE_RECEIVABLE(CustomerStatementBaseEnum.BALANCE_RECEIVABLE.getAttr(),"enum.customerStatementEnum.attrName.balanceReceivable","balanceReceivable",FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9870,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //回款类型 --针对于应收款单据
    PAYMENT_TYPE(CustomerStatementBaseEnum.PAYMENT_TYPE.getAttr(),"enum.customerStatementEnum.attrName.paymentType","paymentType",FieldTypeEnum.TEXT.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9860,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),


    /**
     * 宽表字段，与data同级：
     *  SYS_LONG_1代表对账单据的业务类型，与BUSINESS_TYPE对应
     *  SYS_LONG_2代表对账单据的id，与STATEMENT_ID对应
     */
    SYS_LONG_1(CustomerStatementBaseEnum.SYS_LONG_1.getAttr(),"enum.customerStatementEnum.attrName.businessType","sysLong1",FieldTypeEnum.NUM.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9860,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    SYS_LONG_2(CustomerStatementBaseEnum.SYS_LONG_2.getAttr(),"enum.customerStatementEnum.attrName.statementId","sysLong2",FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(),SetTypeEnum.DISABLE_ALL.getCode(),1,0,ShowTypeEnum.ALL.getCode(),9850,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),

    PRODUCT(CustomerStatementBaseEnum.PRODUCT.getAttr(), "enum.customerStatementEnum.attrName.product", "products", FieldTypeEnum.SELECT_PRODUCT.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    ADD_TIME(CustomerStatementBaseEnum.ADD_TIME.getAttr(), "enum.customerStatementEnum.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.NONE.getCode(), 9790, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    UPDATE_TIME(CustomerStatementBaseEnum.UPDATE_TIME.getAttr(), "enum.customerStatementEnum.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.NONE.getCode(), 9780, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ;


    CustomerStatementEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable, Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }

    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 详见
     *
     * @see ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;
    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    public static String getEsAttr(CustomerStatementEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.getFieldType());
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }


    public static List<String> getEsAttrList(List<CustomerStatementEnum> cacheList) {
        List<String> list = new ArrayList<>();
        for (CustomerStatementEnum cache : cacheList) {
            FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.getFieldType());
            //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
            boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
            String attr= "data." + cache.getAttr();
            if (flag) {
                attr = attr + ".keyword";
            }
            list.add(attr);
        }
        return list;
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    public String getI18nAttrName() {
        return attrName;
    }
}
